﻿
namespace Sai.Core.Rules
{
    using Sai.Core.Introspection;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class CheckConstantValues : IClassRule
    {
        public void Validate(ClassDeclaration was, ClassDeclaration isNow, ITypeDifference typeDifference)
        {
            var isNowFields = isNow.Fields.Where(f => f.IsConstant);

            var reporter = new FieldDifferenceReporter(typeDifference);

            if (isNowFields.Any())
            {
                reporter.ReportOnObsoleteFields(isNowFields, TypeChangeReason.ConstantObsolete);
            }

			// look for missing fields
            var wasFields = was.Fields.Where(f => f.IsConstant);

            if (wasFields.Any())
            {
                reporter.ReportOnRemovedFields(wasFields, isNowFields, TypeChangeReason.ConstantRemoved);
            }

            if (isNowFields.Any())
            {
                reporter.ReportOnAddedFields(wasFields, isNowFields, TypeChangeReason.ConstantAdded);
            }

            if (wasFields.Any() || isNowFields.Any())
            {
                reporter.ReportOnChangedFields(wasFields, isNowFields, TypeChangeReason.ConstantChanged);
            }
        }
    }

}

